package com.aioz.security.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.aioz.security.aop.annotation.LogInfoAnnotation;
import com.aioz.security.common.ResponseResult;
import com.aioz.security.entity.User;
import com.aioz.security.service.impl.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    private UserServiceImpl userService;

    //查询全部用户
    @PostMapping("/get/users")
    @LogInfoAnnotation
    public ResponseResult getAllUsers(@RequestParam(defaultValue = "1") Integer currentPage,
                                      @RequestParam(defaultValue = "10") Integer size){
        return userService.getAllUsers(currentPage,size);
    }
    //获取当前登录用户信息
    @GetMapping("/current")
    public ResponseResult getCurrentUser(){
      return userService.getCurrentUser();
    }

    //查询在线用户
    @GetMapping("/online")
    @LogInfoAnnotation
    public ResponseResult onlineUsers(){
        return userService.onlineUsers();
    }

    //强制用户下线
    @PostMapping("/forced/offline/{id}")
    @LogInfoAnnotation
    public ResponseResult forcedOffline(@PathVariable Long id){
        return userService.forcedOffline(id);
    }

    //修改用户信息
    @PostMapping("/update/info")
    @LogInfoAnnotation
    public ResponseResult updateInfo(@RequestBody User user){
        log.info("ID为 {} 用户更新信息",user.getId());
        return userService.updateUserInfo(user);
    }

    //导出用户数据
    @LogInfoAnnotation
    @GetMapping(value = "/export",produces = "application/octet-stream")
    public void exportUser(HttpServletResponse response){
        List<User> users = userService.list();
        ExportParams params = new ExportParams("用户表","用户表", ExcelType.HSSF);
        Workbook workbook = ExcelExportUtil.exportExcel(params, User.class, users);
        ServletOutputStream out = null;
        try {
            //流形式
            response.setHeader("content-type","application/octet-stream");
            //防止中文乱码
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename="
                    + URLEncoder.encode("用户表.xls", StandardCharsets.UTF_8));
             out = response.getOutputStream();
             workbook.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(null != out){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 导入用户数据
    @PostMapping("/import")
    @LogInfoAnnotation
    public ResponseResult importUsers(MultipartFile file){
        ImportParams params = new ImportParams();
        //去掉标题行
        params.setTitleRows(1);
        try {
            List<User> users = ExcelImportUtil.importExcel(file.getInputStream(), User.class, params);
            if(userService.saveBatch(users)){
                return new ResponseResult(200,"导入成功!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResponseResult(200,"导入失败!");
    }

    //查询求职者简历详细信息
    @PostMapping("/get/resume/{id}")
    @LogInfoAnnotation
    public ResponseResult getResumeInfo(@PathVariable Integer id){
        return userService.getResumeInfo(id);
    }

    //查看自己简历信息
    @GetMapping("/get/own")
    @LogInfoAnnotation
    public ResponseResult getOwnInfo(){
        return userService.getOwnInfo();
    }

    @GetMapping("/company/user")
    @LogInfoAnnotation
    public ResponseResult getCompanyUser(){
        return userService.getCompanyUser();
    }

    //根据用户名查询用户
    @PostMapping("/query/byName/{name}/{companyId}")
    @LogInfoAnnotation
    public ResponseResult queryUserByName(@PathVariable String name,
                                          @PathVariable Integer companyId){
        return userService.queryUserByName(name,companyId);
    }

    //拉黑该用户
    @PostMapping("/forbid/{id}")
    @LogInfoAnnotation
    public ResponseResult forbidUser(@PathVariable Integer id){
        log.info("ID为 {} 的用户被禁用",id);
        return userService.forbidUser(id);
    }
}
